package org.example.service.impl;

import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.ClazzMapper;
import org.example.mapper.EmpMapper;
import org.example.mapper.StudentMapper;
import org.example.pojo.*;
import org.example.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;


    /**
     * 统计各个职位员工人数
     */
    @Override
    public JobOption getEmpJobData() {
        List<Map<String,Object>> list = empMapper.countEmpJobData();
        log.info("查询结果：{}",list);
        List<Object> jobList = list.stream().map(m -> m.get("pos")).toList() ;
        List<Object> dataList = list.stream().map(m -> m.get("total")).toList() ;

        return new JobOption(jobList,dataList);
    }

    /**
     * 统计员工性别信息
     * @return
     */
    @Override
    public List<Map> getEmpGenderData() {
        return empMapper.countEmpGenderData();
    }

    /**
     * 统计班级学生信息
     * @return
     */
    @Override
    public ClazzOption getStudentCountData() {
        List<Clazz> clazzList1 = clazzMapper.selectAll();
        Set<Integer> clazzIds = clazzList1.stream().map(s -> s.getId()).collect(Collectors.toSet());
        Map<String,Integer> map = new HashMap<>();
        List<Object> clazzList = new ArrayList<>();
        List<Object> dataList = new ArrayList<>();
        for (Integer clazzId : clazzIds) {
            clazzList.add(clazzMapper.selectNameById(clazzId));
            dataList.add(studentMapper.selectGroupByClazzId(clazzId));
        }
        return new ClazzOption(clazzList,dataList);
    }

    /**
     * 统计班级学生学历信息
     * @return
     */
    @Override
    public List<Map> getStudentDegreeData() {
        return studentMapper.selectGroupByDegree();
    }

    /**
     * 获取操作日志
     * @param operateLogParam
     * @return
     */
    @Override
    public PageResult logPage(OperateLogParam operateLogParam) {
        PageHelper.startPage(operateLogParam.getPage(),operateLogParam.getPageSize());
        List<OperateLog> logList = empMapper.logPage(operateLogParam);
        return new PageResult(logList.size(),logList);
    }
}
